<?php

namespace App\Admin\Forms;

use App\Models\Category;
use App\Models\Order;
use App\Models\OrderService;
use App\Models\Project;
use App\Models\Todo;
use App\Models\User;
use App\Support\Data;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Form as DcatForm;


class ProjectPerfectForms extends Form implements LazyRenderable
{
    use LazyWidget; // 使用异步加载功能

    // 处理请求
    public function handle(array $input)
    {
        // 获取外部传递参数
        $id = $this->payload['id'] ?? null;

        if (!$id) {
            return $this->response()->error('参数错误');
        }

        $input['check_list_image'] = httpToHttps($input['check_list_image']);
        $result = Project::updateOrCreate(['id' => $id], $input);

        if ($result) {
            return $this->response()->success('成功')->refresh();
        } else {
            return $this->response()->error('失败');
        }
    }

    public function form()
    {
        // 获取外部传递参数
        $id = $this->payload['id'] ?? null;

        $this->datetimeRange('project_start_time', 'project_end_time', '开始结束时间')->required();
        $this->datetime('submit_info_end_time', '提交资料结束时间')->required();
        $this->image('check_list_image', '签到名单')->saveFullUrl()->move('project/check_list_image' . date('Y/m'))->autoUpload()->required();
        $this->text('meeting_address', '会议地址')->required();
        $this->number('number_of_attendees', '讲者人数')->required();
        $this->hidden('id')->value($id);
    }

    // 返回表单数据，如不需要可以删除此方法
    public function
    default()
    {
        // 获取外部传递参数
        $id = $this->payload['id'] ?? null;
        $project = Project::find($id);
        return [
            'project_start_time' => isset($project['project_start_time']) && !empty($project['project_start_time']) ? $project['project_start_time'] : '',

            'project_end_time' => isset($project['project_end_time']) && !empty($project['project_end_time']) ? $project['project_end_time'] : '',

            'submit_info_end_time' => isset($project['submit_info_end_time']) && !empty($project['submit_info_end_time']) ? $project['submit_info_end_time'] : '',

            'check_list_image' => isset($project['check_list_image']) && !empty($project['check_list_image']) ? $project['check_list_image'] : '',

            'meeting_address' => isset($project['meeting_address']) && !empty($project['meeting_address']) ? $project['meeting_address'] : '',

            'number_of_attendees' => isset($project['number_of_attendees']) && !empty($project['number_of_attendees']) ? $project['number_of_attendees'] : '',
        ];
    }
}
